package com.ff.kvm.common.third_util;

import com.aliyun.oss.OSSClient;

import java.io.FileInputStream;
import java.util.UUID;

public class AliyunOSSUtil {

    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAIi9hQC72WfTJ9";
    private static String accessKeySecret = "W381iqTAfXgFYBvigNBP4UmRtLilsQ";
    private static String bucketName = "kvm-mall";
    private static String url = "http://kvm-mall.oss-cn-hangzhou.aliyuncs.com/";

    public static OSSClient getOSSClient(){

        return new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }
    public static String upload(FileInputStream fileInputStream, String name){

        String fileType = name.substring(name.lastIndexOf("."));
        String fileName = UUID.randomUUID() + fileType;
        // Endpoint以杭州为例，其它Region请按实际情况填写。
//        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
//        String accessKeyId = "<yourAccessKeyId>";
//        String accessKeySecret = "<yourAccessKeySecret>";
//        String bucketName = "kvm-product";
//        String objectName = "<yourObjectName>";
        OSSClient ossClient = getOSSClient();
        // 创建OSSClient实例。
//        ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        // 上传内容到指定的存储空间（bucketName）并保存为指定的文件名称（objectName）。
        ossClient.putObject(bucketName, fileName, fileInputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
//        StringBuffer url = new StringBuffer(endpoint);

        return url + fileName;
    }
}
